package com.czk.scala.chapter04

import scala.util.control.Breaks

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/7 16:11
 */
object Test04_Break {
  def main(args: Array[String]): Unit = {
    // 1 采用抛出异常的方式，退出循环
    try {
      for (i <- 0 until 5) {
        if (i == 3) {
          throw new RuntimeException
        }
        println(i)
      }
    } catch {
      case e: Exception =>
    }
    println("循环外的代码")

    // 使用scala中的Break类的break方法，实现异常的抛出与捕捉
    for (i <- 0 until 5) {
      if (i == 3) {
        Breaks.break()
      }
      println(i)
    }
  }
}
